package com.bid;

import org.anddev.andengine.engine.camera.hud.HUD;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.util.modifier.IModifier;
import org.anddev.andengine.util.modifier.IModifier.IModifierListener;
import org.anddev.andengine.util.modifier.ease.EaseStrongOut;
import org.anddev.andengine.util.modifier.ease.IEaseFunction;

public class SidePopUp extends Sprite {
	private HUD hud;
	private static float ratio = BID.ratioMedium + 0.5f;
	private TiledTextureRegion ChildrenTextureRegion[];
	
	public SidePopUp(float pX, float pY, float pWidth, float pHeight, TextureRegion pTextureRegion, TiledTextureRegion pChildrenTextureRegion[]) {
		super(pX * ratio, pY * (ratio - 1f), pWidth * ratio, pHeight * ratio, pTextureRegion);
		this.ChildrenTextureRegion = pChildrenTextureRegion;
	}

	public void setHUD(final HUD pHud){
		this.hud = pHud;
	}
	
	public void createItems(){
		MenuItem next = new MenuItem(32, 16, ChildrenTextureRegion[0]){
			@Override
			public void doStuff(){
				if(LevelPicker.levelSelected == 9 * 3 - 1){
					return ;//Game Over
				}
				LevelPicker.levelSelected ++;
				LevelPicker.unlockPending = LevelPicker.levelSelected;
				slideLeft();
				if((LevelPicker.levelSelected) % 9 == 0){
					GameCore.quitPending = true;
				}
				else{
					GameCore.deleteAllPending = true;
					GameCore.newGamePending = true;
				}
			}
		};
		MenuItem restart = new MenuItem(32, 80, ChildrenTextureRegion[1]){
			@Override
			public void doStuff(){
				slideLeft();
				GameCore.restartPending = true;
			}
		};
		MenuItem quit = new MenuItem(32, 160, ChildrenTextureRegion[2]){
			@Override
			public void doStuff(){
				slideLeft();
				GameCore.quitPending = true;
			}
		};
		this.attachChild(next);
		this.attachChild(restart);
		this.attachChild(quit);
		this.hud.registerTouchArea(next);
		this.hud.registerTouchArea(restart);
		this.hud.registerTouchArea(quit);
		this.hud.registerTouchArea(this);
		
	}
	
	public void slideRight(){
		this.setVisible(true);
		this.setIgnoreUpdate(false);
		IEaseFunction efRight = EaseStrongOut.getInstance();
		this.clearEntityModifiers();
		this.registerEntityModifier(new MoveModifier(1.5f, this.getX(), 0, this.getY(), this.getY(), efRight));
	}
	
	public void slideLeft(){
		this.setPosition(-130 * ratio, this.getY());
		this.setVisible(false);
		this.setIgnoreUpdate(true);
	}
	
	@Override
	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
		return true;
	}
	
	/*Inner Class*/
	private abstract class MenuItem extends AnimatedSprite{
		private boolean isDown = false;
		public MenuItem(float pX, float pY, TiledTextureRegion pTiledTextureRegion) {
			super(pX * ratio, pY * ratio,64 * ratio, 64 * ratio, pTiledTextureRegion);
		}
		@Override
		public boolean contains(final float x, final float y){
		 if(!super.contains(x, y)){
			 this.isDown = false;
			 this.setCurrentTileIndex(0);
             return false;
         }
		 
         return true;
		}
		@Override
		public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
			if(pSceneTouchEvent.isActionDown()){
				this.isDown = true;
				this.setCurrentTileIndex(1);
				return true;
			}
			if(pSceneTouchEvent.isActionUp() && this.isDown){
				doStuff();
				
				this.isDown = false;
				this.setCurrentTileIndex(0);
				return true;
			}
			return false;
		}
		
		public abstract void doStuff();
	}
}
